package com.mftour.moudles.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mftour.common.cache.InitCache;
import com.mftour.moudles.entity.ConsolePermission;
import com.mftour.moudles.entity.ConsolePermissionDetail;
import com.mftour.moudles.mapper.ConsolePermissionMapper;
import com.mftour.moudles.service.ConsolePermissionService;

@Service
public class ConsolePermissionServiceImpl extends BaseServiceImpl<ConsolePermission,Integer> implements ConsolePermissionService {
//	@Autowired
//	consolePermissionMapper consolePermissionMapper;	
	@Autowired
	ConsolePermissionMapper consolePermissionMapper;

	@Override
	public List<ConsolePermissionDetail> selectMenuPermissionByUserId(Integer userId) {
		return consolePermissionMapper.selectMenuPermissionByUserId(userId);
	}

	@Override
	public void refreshAllPermissions() {

		InitCache.allCategoryMap.clear();
		InitCache.allCategoryList.clear();
		InitCache.allPermissionsMap.clear();
		InitCache.allPermissionsList.clear();

		List<ConsolePermission> allPermissions = selectListSelective(null);

		for (ConsolePermission consolePermission : allPermissions) {
			InitCache.allPermissionsMap.put(consolePermission.getPermissionId(), consolePermission);
			if (consolePermission.getPermissionType().equals("0")) {
				try {
					InitCache.allCategoryMap.put(consolePermission.getPermissionId(), (ConsolePermission) consolePermission.clone());
				} catch (CloneNotSupportedException e) {
					e.printStackTrace();
				}
			}
		}

		for (Map.Entry<Integer, ConsolePermission> userEntry : InitCache.allPermissionsMap.entrySet()) {
			ConsolePermission cp = userEntry.getValue();
			if (cp.getParentId().intValue() != 0) {
				InitCache.allPermissionsMap.get(cp.getParentId()).getPermissions().add(cp);
			} else {
				InitCache.allPermissionsList.add(cp);
			}
		}

		sortMenuPermission(InitCache.allPermissionsList);

		for (Map.Entry<Integer, ConsolePermission> userEntry : InitCache.allCategoryMap.entrySet()) {
			ConsolePermission cp = userEntry.getValue();
			if (cp.getParentId().intValue() != 0) {
				InitCache.allCategoryMap.get(cp.getParentId()).getPermissions().add(cp);
			} else {
				InitCache.allCategoryList.add(cp);
			}
		}

		changePermissionLevelName(InitCache.allCategoryList,1);
		sortMenuPermission(InitCache.allCategoryList);
	}
	
	private void changePermissionLevelName(List<ConsolePermission> cps, int level) {
		StringBuilder sb = new StringBuilder();
		for (ConsolePermission cp : cps) {
			sb.setLength(0);
			for (int i = 0; i < level; i++) {
				sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			}
			sb.append(cp.getPermissionName());
			cp.setPermissionName(sb.toString());
			if (cp.getPermissions() != null && cp.getPermissions().size() > 0) {
				changePermissionLevelName(cp.getPermissions(), level + 1);
			}
		}
	}

	@Override
	public void refreshAllUserMenuPermissions() {

		InitCache.allUserMenuPermissions.clear();
		InitCache.allUserMenuMap.clear();
		
		Map<Integer, Map<Integer, ConsolePermissionDetail>> allUserMenuPermissionsMap = new HashMap<Integer, Map<Integer, ConsolePermissionDetail>>();
		List<ConsolePermissionDetail> allMenuPermissions = selectMenuPermissionByUserId(null);
		for (ConsolePermissionDetail consolePermissionDetail : allMenuPermissions) {
			if (allUserMenuPermissionsMap.get(consolePermissionDetail.getUserId()) == null) {
				Map<Integer, ConsolePermissionDetail> tmp = new HashMap<Integer, ConsolePermissionDetail>();
				tmp.put(consolePermissionDetail.getPermissionId(), consolePermissionDetail);
				allUserMenuPermissionsMap.put(consolePermissionDetail.getUserId(), tmp);
			} else {
				allUserMenuPermissionsMap.get(consolePermissionDetail.getUserId()).put(consolePermissionDetail.getPermissionId(), consolePermissionDetail);
//				allUserMenuPermissionsMap.put(consolePermissionDetail.getUserId(), allUserMenuPermissionsMap.get(consolePermissionDetail.getUserId()));
			}
			
			if (consolePermissionDetail.getPermissionType().equals("1")) {
				if (InitCache.allUserMenuMap.get(consolePermissionDetail.getUserId()) == null) {
					Map<String, Integer> tmp = new HashMap<String, Integer>();
					tmp.put(consolePermissionDetail.getPermissionUrl(), 1);
					InitCache.allUserMenuMap.put(consolePermissionDetail.getUserId(), tmp);
				}else{
					InitCache.allUserMenuMap.get(consolePermissionDetail.getUserId()).put(consolePermissionDetail.getPermissionUrl(), 1);
//					InitCache.allUserMenuMap.put(consolePermissionDetail.getUserId(), InitCache.allUserMenuMap.get(consolePermissionDetail.getUserId()));
				}
			}
		}

		for (Map.Entry<Integer, Map<Integer, ConsolePermissionDetail>> entry : allUserMenuPermissionsMap.entrySet()) {
			List<ConsolePermission> userPermissionList = new ArrayList<ConsolePermission>();
			for (Map.Entry<Integer, ConsolePermissionDetail> userEntry : entry.getValue().entrySet()) {
				ConsolePermissionDetail cpd = userEntry.getValue();
				if (cpd.getParentId().intValue() != 0) {
					entry.getValue().get(cpd.getParentId()).getPermissions().add(cpd);
				} else {
					userPermissionList.add(cpd);
				}
			}
			sortMenuPermission(userPermissionList);
			InitCache.allUserMenuPermissions.put(entry.getKey(), userPermissionList);
		}
	}
	
	public void sortMenuPermission(List<ConsolePermission> cpList){
		Collections.sort(cpList);
		for(ConsolePermission cp:cpList){
			if(cp.getPermissions()!=null){
				sortMenuPermission(cp.getPermissions());
			}
		}
	}

	@Override
	public String checkDelAble(Integer permissionId){		
		ConsolePermission cp= InitCache.allPermissionsMap.get(permissionId);
		if(cp==null){
			return "该权限不存在";
		}else		if(cp.getPermissionType().equals("0")&&cp.getPermissions().size()>0){
			return "该权限下面有其他权限！";
		}
		return "";
	}
	
	@Override
	public ConsolePermission getConsolePermissionById(Integer permissionId) {
		ConsolePermission cp= InitCache.allPermissionsMap.get(permissionId);
		if(cp!=null){
			ConsolePermission result = null;
			try {
				result = (ConsolePermission) cp.clone();
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
			return result;
		}
		ConsolePermission record = selectByPrimaryKey(permissionId);
		return record;
	}
}